package top.superflow.version.db.data

import org.apache.commons.lang3.StringUtils
import top.superflow.db.api.DBField
import top.superflow.exception.RequestUnsupportedException
import top.superflow.log.Loggable
import top.superflow.verify.Ensure
import top.superflow.version.compare.suggestion.Suggestion
import top.superflow.version.db.{FieldPropertyVersion, Version}

import scala.reflect.runtime.universe.MethodSymbol
import scala.reflect.runtime.{universe => ru}

class DBFieldPropertyVersion (propName : String, propValue : Any) extends FieldPropertyVersion with Ensure{
  
  notEmpty(propName, "propName should not be null")
  
  
  
  this.name.compareAndSet(this.name.get(), propName)
  this.value.compareAndSet(this.value.get, propValue.asInstanceOf[AnyRef])
  
  
  
  def getId : String = {
    this.name.get
  }
  
  def compare(targetStructure: Any): Option[Suggestion] = ???
  def getMatchedChild(childDataVersionKey: Any): Option[Version] = ???
  
  def getPublicName = {
    DBFieldPropertyVersion.getPublicName(this.name.toString())
  }
}

object DBFieldPropertyVersion extends Ensure with Loggable{
  
  def get(getter : MethodSymbol, field : DBField) : Any = {
    notNull(getter, "The method getter should not be null.")
    notNull(field, "The field instance should not be null.")
    ru.runtimeMirror(field.getClass.getClassLoader).reflect(field).reflectMethod(getter)()
  }
  
  def getPublicName(originalName : String) : String = {
    notEmpty(originalName, "originalName should not be null")

    originalName match {
      case x if(StringUtils.equals(x, FieldPropertyVersion.DB_FIELD_COLUMN_NAME)) => {
        FieldPropertyVersion.FIELD_COLUMN_NAME
      }
      
      case x if(StringUtils.equals(x, FieldPropertyVersion.DB_FIELD_INSERTABLE)) => {
        FieldPropertyVersion.FIELD_INSERTABLE
      }
      
      case x if(StringUtils.equals(x, FieldPropertyVersion.DB_FIELD_LENGTH)) => {
        FieldPropertyVersion.FIELD_LENGTH
      }
      
      case x if(StringUtils.equals(x, FieldPropertyVersion.DB_FIELD_NULLABLE)) => {
        FieldPropertyVersion.FIELD_NULLABLE
      }
      
      case x if(StringUtils.equals(x, FieldPropertyVersion.DB_FIELD_PRECISION)) => {
        FieldPropertyVersion.FIELD_PRECISION
      }
      
      case x if(StringUtils.equals(x, FieldPropertyVersion.DB_FIELD_SCALE)) => {
        FieldPropertyVersion.FIELD_SCALE
      }
      
      case x if(StringUtils.equals(x, FieldPropertyVersion.DB_FIELD_UNIQUE)) => {
        FieldPropertyVersion.FIELD_UNIQUE
      }
      
      case x if(StringUtils.equals(x, FieldPropertyVersion.DB_FIELD_UPDATABLE)) => {
        FieldPropertyVersion.FIELD_UPDATABLE
      }
      
      case x if(StringUtils.equals(x, FieldPropertyVersion.DB_FIELD_TYPE)) => {
        FieldPropertyVersion.FIELD_TYPE
      }
      
      case _ => {
        throw new RequestUnsupportedException("originalName " + originalName + " is not supported")
//        logger.warn("The entity has already existed.")
      }
    }
  }
}